package com.channelsharing.hongqu.portal.api.controller.coupon;

import com.channelsharing.common.exception.SystemInnerBusinessException;
import com.channelsharing.hongqu.portal.api.controller.BaseController;
import com.channelsharing.hongqu.portal.api.entity.Coupon;
import com.channelsharing.hongqu.portal.api.entity.UserCoupon;
import com.channelsharing.hongqu.portal.api.enums.UserCouponStatus;
import com.channelsharing.hongqu.portal.api.service.CouponService;
import com.channelsharing.hongqu.portal.api.service.UserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "优惠券功能")
@Validated
@RestController
@RequestMapping("/v1/coupon/")
public class CouponController extends BaseController {

    @Autowired
    private CouponService couponService;
    @Autowired
    private UserCouponService userCouponService;

    @ApiOperation(value = "获取某个商品所有的优惠券列表")
    @GetMapping("getByGoodsId")
    public List<Map<String,Object>> list(@RequestParam Long goodsId) {
        List<Map<String,Object>> maps=new ArrayList<>();
        Coupon couponEntity =new Coupon();
        couponEntity.setGoodsId(goodsId);
        couponEntity.setSendEndDate(new Date());
        couponEntity.setUseEndDate(new Date());

        List<Coupon> coupons = couponService.findUsableList(goodsId);
        for(Coupon coupon:coupons){
            Map<String,Object> map=new HashMap<>();
            List<UserCoupon> userCoupons=userCouponService.findListByCouponId(coupon.getId());
            if(userCoupons!=null&&userCoupons.size()!=0){
                map.put("status", 1);
            }else {
                map.put("status", 0);
            }
            map.put("coupon",coupon);
            maps.add(map);
        }
        return maps;
    }

    @ApiOperation(value = "获取我的优惠券列表0:未使用 1：已使用 2：已过期")
    @GetMapping("/list")
    public List<UserCoupon> list(Integer status) {
        Long userId = currentUserId();
        List<UserCoupon> userCoupons = userCouponService.findByUserIdAndStatus(userId,status);
        return userCoupons;
    }

    /**
     * 领取优惠券
     * @param id
     */
    @ApiOperation(value = "领取优惠券")
    @GetMapping("receive")
    public void receive( Long id){
        UserCoupon entity = new UserCoupon();
        entity.setCouponId(id);
        entity.setUserId(currentUserId());
        entity.setStatus(UserCouponStatus.unused.getCode());
        UserCoupon userCoupon = userCouponService.findOne(entity);
        if(userCoupon != null){
            throw new SystemInnerBusinessException("同种优惠券只能领取一张");
        }
        Coupon couponEntity = new Coupon();
        couponEntity.setId(id);
       Coupon coupon= couponService.findOne(couponEntity);

        userCoupon = new UserCoupon();
        userCoupon.setUserId(currentUserId());
        userCoupon.setCouponId(id);
        userCoupon.setStatus(UserCouponStatus.unused.getCode());
        userCoupon.setCouponName(coupon.getName());

        userCouponService.add(userCoupon);
    }
}
